import {routes} from './router'
import Vue from 'vue'
import Router from 'vue-router'
import VueRouter from 'vue-router'
import {get} from 'lodash'
import {doAuth} from './util/help'
import store from './store/index'
import request from './api/request'

Vue.use(Router)

const myRoutes = [
  ...routes
]

export const router = new VueRouter({
  mode: 'hash',
  routes: myRoutes,
  scrollBehavior(to, from, savedPosition) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        console.log('scrollBehavior')
        resolve({x: 500, y: 0})
      }, 2000)
    })
  }
})

// beforeRouteUpdate (to, from, next) {
//   console.log(to.matched[0].path)
//   console.log(from.matched[0].path)
//   const toPath=to.matched[0].path
//   const fromPath=from.matched[0].path
//   if (toPath===fromPath) {
//     console.log('手动刷新一次页面')
//     // console.log($el)
//   }
//   // console.log(to.fullPath.split('/'))
//   // console.log(from.fullPath.split('/'))
//   //lodash difference
//   next()
//   // react to route changes...
//   // don't forget to call next()
// }

router.beforeEach((to, from, next) => {
  console.log('第一个全局守卫')
  if (!get(to, 'meta.public') && !doAuth()) {
    console.log('请重新登陆')
    // router.push('login')
    router.replace('login')
  } else {
    if (to.meta.title) {
      document.title = to.meta.title
    }
    // if (!to.meta.noLoading) {
    //   request.interceptors.request.use(config => {
    //     store.commit('countRequest')
    //     return {...config}
    //   })
    //   request.interceptors.response.use(response => {
    //     store.commit('countResponse')
    //     return response
    //   }, error => {
    //     store.commit('responseError')
    //     return Promise.reject(error)
    //   })
    // }


    // console.log(true && 'kobe')
    // console.log(false || 'kobe')
    // const toPath = to.matched[0].path
    // const toPath = get(to, 'matched[0].path')
    // const fromPath = get(from, 'matched[0].path')
    // // console.log(toPath)
    // if (toPath && fromPath && toPath === fromPath) {
    //   console.log('同一个页面-手动刷新一下')
    //   // console.log(Vue)
    //   // Vue.forceUpdate()
    //   // Vue.nextTick(()=>{
    //   //   console.log('刷新一下')
    //   // })
    // }

    // console.log(from.fullPath.split('/'))
    //lodash difference
    next()
    // console.log(to)
    // this.$router.push(to.fullPath)
    // console.log('to'+to)
    // console.log('from'+from)
    // console.log('next'+next)
  }
})

// router.beforeEach((to, from, next) => {
//   if (to.matched.some(record => record.meta.requiresAuth)) {
//     // this route requires auth, check if logged in
//     // if not, redirect to login page.
//     if (true) {
//       next({
//         path: '/login',
//         query: { redirect: to.fullPath }
//       })
//     } else {
//       next()
//     }
//   } else {
//     next() // 确保一定要调用 next()
//   }
// })

router.beforeEach((to, from, next) => {
  const doAuth = true
  if (get(to, 'meta.requiresAuth')) {
    console.log('不需要鉴权')
    next()
  } else {
    console.log('需要鉴权')
    if (doAuth) {
      next()
    } else {
      next()
    }
  }
  console.log(from)
  console.log('第二个全局守卫')
  next()
  // next(false)
})

router.beforeResolve(
  (to, from, next) => {
    console.log('全局解析守卫')
    next()
    /*console.log('')
    console.log('')
    console.log('')
    console.log('')
    console.log('')
    console.log('')
    console.log('')
    console.log('')*/
  }
)

router.afterEach((to, from) => {
  console.log('全局后置钩子')
})
